//
//  SISSSettingsViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/4.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSSettingsViewController.h"
#import "SISSTabBarController.h"
#import "SISSLoginViewController.h"
#import <Hyphenate/EMClient.h>
#import <JPUSHService.h>

@interface SISSSettingsViewController ()
@property (weak, nonatomic) IBOutlet UILabel *cacheLabel;

@end

@implementation SISSSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cacheLabel.text = [NSString stringWithFormat:@"%.2fM",[self getFilesize]];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
//    SISSTabBarController *tabbar = (SISSTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//    tabbar.customTabBar.hidden = YES;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark-清除缓存
- (IBAction)clearCacheClickAction:(id)sender {
    
    [self clearFile];
}

// 显示缓存大小
- (float)getFilesize {
    
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    return [self folderSizeAtPath :cachPath];
}

// 1:首先我们计算一下 单个文件的大小
- (long long)fileSizeAtPath:(NSString *)filePath {
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    if ([manager fileExistsAtPath :filePath]) {
        
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

// 2:遍历文件夹获得文件夹大小
- ( float)folderSizeAtPath:(NSString *)folderPath {
    
    NSFileManager *manager = [ NSFileManager defaultManager];
    
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator];
    
    NSString *fileName;
    
    long long folderSize = 0 ;
    
    while ((fileName = [childFilesEnumerator nextObject]) != nil ) {
        
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    
    return folderSize/( 1024.0 * 1024.0 );
}

// 清理缓存

- (void)clearFile {
    
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    NSArray * files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
    
    NSLog ( @"cachpath = %@" , cachPath);
    
    for (NSString * p in files) {
        
        NSError * error = nil ;
        
        NSString * path = [cachPath stringByAppendingPathComponent :p];
        
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            
            [[NSFileManager defaultManager] removeItemAtPath:path error :&error];
        }
    }
    
    [self performSelectorOnMainThread:@selector(clearCachSuccess) withObject:nil waitUntilDone : YES ];
}

- (void)clearCachSuccess {
    self.cacheLabel.text = @"0M";
    
    [SISSTools showSVSuccessTitle:@"清理完毕！" andTime:1 andColor:YES];
}

/**
 退出按钮点击事件
 */
- (IBAction)exitButtonClickAction:(id)sender {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"退出" message:@"您确定要退出当前帐户吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[EMClient sharedClient] logout:YES completion:^(EMError *aError) {
            [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                
                NSLog(@"rescode: %ld, \ntags: %@, \nalias: %@\n", (long)iResCode, @"tag" , iAlias);
               
            } seq:1];
            // 更新当前账号
//            [SISSUserDefaultsTool saveObject:nil forKey:CURRENTID];
            // 更新用户登录状态
            [SISSUserDefaultsTool saveObject:nil forKey:CURRENTLOGIN];
            [SISSUserAccount shared].access_token = nil;
            [SISSUserAccount shared].token_type = nil;
            [UIApplication sharedApplication].keyWindow.rootViewController = [[SISSLoginViewController alloc] init];
        }];
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:confirmAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
